package com.pgytesting.secondhandcar.controller.mcpuser;

import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.service.McpUserInfoService;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author : frosty0804@gmail.com
 * @version : v1.0
 * @projectName : SecondHandCar
 * @className : McpUserInfoController
 * @description : TODO(用户信息Controller)
 * @date : 2019/8/12 19:10
 */
@RestController
@RequestMapping("/user/login")
public class LoginMcpUserInfoController {

    @Autowired
    McpUserInfoService mcpUserInfoService;

    /**
     * 用户登录
     *
     * @param mcpUserInfo   用户信息
     * @param bindingResult 参数校验
     * @param httpSession   session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseMessage login(@Valid @RequestBody McpUserInfo mcpUserInfo, BindingResult bindingResult, HttpSession httpSession) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }

        return mcpUserInfoService.queryLogin(mcpUserInfo);
    }


    /**
     * 用户注册
     *
     * @param mcpUserInfo   注册信息
     * @param bindingResult 参数校验
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseMessage register(@Valid @RequestBody McpUserInfo mcpUserInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }
        return mcpUserInfoService.register(mcpUserInfo);
    }

}
